﻿/*根据id获取对象*/
function $(str) {
    return document.getElementById(str);
}

var addrShow = $('addr-show');
var btn = document.getElementsByClassName('met1')[0];
var prov = $('prov');
var city = $('city');
var country = $('country');


/*用于保存当前所选的省市区*/
var current = {
    prov: '',
    city: '',
    country: ''
};

/*自动加载省份列表*/
(function showProv() {
    btn.disabled = true;
    var len = provice.length;
    for (var i = 0; i < len; i++) {
        var provOpt = document.createElement('option');
        provOpt.innerText = provice[i]['name'];
        provOpt.value = i;
        prov.appendChild(provOpt);
    }
})();

/*根据所选的省份来显示城市列表*/
function showCity(obj) {
    var val = obj.options[obj.selectedIndex].value;
    if (val != current.prov) {
        current.prov = val;
        addrShow.value = '';
        btn.disabled = true;
    }
    //console.log(val);
    if (val != null) {
        city.length = 1;
        var cityLen = provice[val]["city"].length;
        for (var j = 0; j < cityLen; j++) {
            var cityOpt = document.createElement('option');
            cityOpt.innerText = provice[val]["city"][j].name;
            cityOpt.value = j;
            city.appendChild(cityOpt);
        }
    }
}

/*根据所选的城市来显示县区列表*/
function showCountry(obj) {
    var val = obj.options[obj.selectedIndex].value;
    current.city = val;
    if (val != null) {
        country.length = 1; //清空之前的内容只留第一个默认选项
        var countryLen = provice[current.prov]["city"][val].districtAndCounty.length;
        if (countryLen == 0) {
            addrShow.value = provice[current.prov].name + '-' + provice[current.prov]["city"][current.city].name;
            return;
        }
        for (var n = 0; n < countryLen; n++) {
            var countryOpt = document.createElement('option');
            countryOpt.innerText = provice[current.prov]["city"][val].districtAndCounty[n];
            countryOpt.value = n;
            country.appendChild(countryOpt);
        }
    }
}

/*选择县区之后的处理函数*/
function selecCountry(obj) {
    current.country = obj.options[obj.selectedIndex].value;
    if ((current.city != null) && (current.country != null)) {
        btn.disabled = false;
    }
}

/*点击确定按钮显示用户所选的地址*/
function showAddr() {
    addrShow.value = provice[current.prov].name + provice[current.prov]["city"][current.city].name + provice[current.prov]["city"][current.city].districtAndCounty[current.country];
}


/*    $("#Name").blur(function () {
        JudgeName();
    });
    $("#Address").blur(function () {
        JudgeAddress();
    });
    $("#Zipcode").blur(function () {
        JudgeZipcode();
    });
    $("#Phone").blur(function () {
        Judgephone();
    });*/
  

function Show() {
    var Name = $("#Name").val();//收件人
    var Address = $("#Address").val();//详细地址
    var Zipcode = $("#Zipcode").val();//邮编
    var phone = document.getElementById('Phone').value;//手机号码

    //验证
    if (JudgeName()) {
        if (JudgeAddress()) {
            if (JudgeZipcode()) {
                if (Judgephone()) {
                    var num = null;
                    num = provice[current.prov].name + provice[current.prov]["city"][current.city].name + provice[current.prov]["city"][current.city].districtAndCounty[current.country] + Address;
                    var data = {};
                    data.Delivery_Name = Name;
                    data.Delivery_Adress = num;
                    data.Delivery_Zipcode = Zipcode;
                    data.Delivery_TelPhone = phone;
                    data.User_Id = $("#UserId").val();
                    $.ajax({
                        url: "/User/AddAddress",
                        type: "post",
                        data: data,
                        success: function (result) {
                            if (result.Success) {
                                window.location.reload();//刷新页面
                                location.href = "/Home/User_Address";
                            }
                            else {
                                alert("失败");
                            }
                        }
                    })
                }
            }
        }
    }




                }

function JudgeName() {
    var Name = $("#Name").val();//收件人
    if (Name == "") {
        $("#num1").text("收件人不能为空!");
        $("#num1").css("color", "red");
        return false;

    } else {
        $("#num1").text("成功!");
        $("#num1").css("color", "green");
        return true;
    }
}
function JudgeAddress() {
    var Address = $("#Address").val();//详细地址
    if (Address == "" && $("#country").val() == "请选择县区") {
        $("#num2").text("地址不能为空!");
        $("#num2").css("color", "red");
    }
    if (Address == "" && $("#country").val() != "请选择县区") {
        $("#num2").text("详细地址不能为空!");
        $("#num2").css("color", "red");
        return false;
    } else if (Address != "" && $("#country").val() == "请选择县区") {
        $("#num2").text("请选择地址!");
        $("#num2").css("color", "red");
        return false;
    } else if (Address != "" && $("#country").val() != "请选择县区") {
        $("#num2").text("成功!");
        $("#num2").css("color", "green");
        return true;
    }
}

function JudgeZipcode() {
    var Zipcode = $("#Zipcode").val();//邮编
    if (Zipcode == "") {
        $("#num3").text("邮编不能为空!");
        $("#num3").css("color", "red");
        return false
        //邮编正则表达式 
     } else if (!(/^(0[1234567]|1[012356]|2[01234567]|3[0123456]|4[01234567]|5[1234567]|6[1234567]|7[012345]|8[013456])\d{4}$/).test(Zipcode)) {
        $("#num3").text("请输入正确的邮编!");
        $("#num3").css("color", "red");
        return false;
    } else {
        $("#num3").text("成功!");
        $("#num3").css("color", "green");
        return true;
    }
}

function Judgephone() {
    var phone = document.getElementById('Phone').value;//手机号码
    if (phone == "") {
        $("#num4").text("手机号码不能为空!");
        $("#num4").css("color", "red");
        return false;
        //手机号码正则表达式
    } else if (!(/^1[3456789]\d{9}$/.test(phone))) {
        $("#num4").text("手机号码有误，请重填!");
        $("#num4").css("color", "red");
        return false;
    } else {
        $("#num4").text("成功!");
        $("#num4").css("color", "green");
        return true;
    }
}

//修改点击事件
function modifyAdress() {
    if ($("#LAdress").val() == "" || $("#LAdress").val() == null) {
        //验证
        if (JudgeName()) {
            if (JudgeAddress()) {
                if (JudgeZipcode()) {
                    if (Judgephone()) {
                        var num = null;
                        num = provice[current.prov].name + provice[current.prov]["city"][current.city].name + provice[current.prov]["city"][current.city].districtAndCounty[current.country] + $("#Address").val();
                        var data = {};
                        data.Delivery_Name = $("#Name").val();
                        data.Delivery_Adress = num;
                        data.Delivery_Zipcode = $("#Zipcode").val();
                        data.Delivery_TelPhone = $("#Phone").val();
                        data.Delivery_Id = $("#Delivery_Id").val();
                        $.ajax({
                            url: "/User/UpdateAddress",
                            type: "post",
                            data: data,
                            success: function (result) {
                                if (result.Success) {
                                    alert("修改成功");
                                    window.location.reload();//刷新页面
                                    $("#BsAdress").hide();
                                    $("#CsAdress").hide();
                                    $("#EsAdress").show();
                                }
                                else {
                                    alert("修改失败");
                                }
                            }
                        })


                    }
                }
            }
        }
    } else {
        //验证
        if (JudgeName()) {
            if (JudgeZipcode()) {
                if (Judgephone()) {
                    var data = {};
                    data.Delivery_Name = $("#Name").val();
                    data.Delivery_Adress = $("#LAdress").val();
                    data.Delivery_Zipcode = $("#Zipcode").val();
                    data.Delivery_TelPhone = $("#Phone").val();
                    data.Delivery_Id = $("#Delivery_Id").val();
                    $.ajax({
                        url: "/User/UpdateAddress",
                        type: "post",
                        data: data,
                        success: function (result) {
                            if (result.Success) {
                                alert("修改成功");
                                window.location.reload();//刷新页面
                            }
                            else {
                                alert("修改失败");
                            }
                        }
                    })


                }
            }
        }
    }





}